const express = require("express");
const router = express.Router();

const db = require("../utils/db");

// 文章接口

// 获取全部文章
router.post("/api/articleadd", (req, res) => {
  let sql = `select * from articles`;
  db.query(sql, (err, result) => {
    if (err) {
      res.json({
        status: 500,
        msg: "服务器内部错误",
      });
    } else {
      res.json({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// 添加文章
router.post("/api/article", (req, res) => {
  const {
    title,
    content,
    author,
    category_id,
    created_at,
    updated_at,
    published_at,
  } = req.body;
  console.log(req.body);
  // 将文章信息保存到数据库
  const sql =
    "INSERT INTO articles (title, content, author, category_id, created_at, updated_at, published_at) VALUES (?, ?, ?, ?, ?, ?, ?)";
  const values = [
    title,
    content,
    author,
    category_id,
    created_at,
    updated_at,
    published_at,
  ];
  db.query(sql, values, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "保存文章信息失败" });
    } else {
      res.json({ message: "保存文章信息成功" });
    }
  });
});

// export const getArticleById = (params) =>request.post("/api/articleById",params);
router.post("/api/articleById", (req, res) => {
  const { id } = req.body;
  console.log(req.body);
  const sql = "SELECT * FROM articles WHERE id = ?";
  db.query(sql, [id], (err, result) => {
    if (err) {
      console.error(err);
      res.send({ code: 500, msg: "获取文章信息失败" });
    } else {
      res.send({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// export const getCategoryById = (params) =>request.post("/api/categoryById",params);

router.post("/api/categoryById", (req, res) => {
  const { id } = req.body;
  const sql = "SELECT * FROM categories WHERE id = ?";
  db.query(sql, [id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取分类信息失败" });
    } else {
      res.json(result);
    }
  });
});

// export const searchApi = (params) =>reque st.post("/api/search",params);
router.post("/api/search", (req, res) => {
  const { searchValue } = req.body;
  let keyword = searchValue;
  const sql = `SELECT * FROM articles WHERE title LIKE '%${keyword}%'`;
  db.query(sql, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "搜索失败" });
    } else {
      res.send({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// export const getTagBytag = (params) =>request.post("/api/tagById",params);
router.post("/api/tagBytag", (req, res) => {
  const { tag } = req.body;
  const name = tag;
  const sql = "INSERT INTO tags (name) VALUES (?)"; // Modify the SQL statement to insert the name
  db.query(sql, [name], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "插入标签信息失败" }); // Update the error message for insertion failure
    } else {
      res.json(result);
    }
  });
});

// export const showTagApi = () => request.post("/api/tagadd");
router.post("/api/tagadd", (req, res) => {
  const sql = "SELECT * FROM tags";
  db.query(sql, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取标签信息失败" });
    } else {
      res.json(result);
    }
  });
});

// export const addCommentApi = (params) => request.post("/api/comment", params);
router.post("/api/comment", (req, res) => {
  const { article_id, user_id, content, created_at, updated_at } = req.body;
  console.log(req.body);
  const sql =
    "INSERT INTO comments (article_id,user_id, content, created_at, updated_at) VALUES (?, ?, ?, DEFAULT, DEFAULT)";
  const values = [article_id, user_id, content, created_at, updated_at];
  db.query(sql, values, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "保存评论信息失败" });
    } else {
      res.json({ message: "保存评论信息成功" });
    }
  });
});

// 删除文章
// export const deletePassageApi = (params) => request.post("/api/articledelete", params);
router.post("/api/articledelete", (req, res) => {
  const { id } = req.body;
  const sql = "UPDATE articles SET is_deleted = 1 WHERE id = ?";
  db.query(sql, [id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "修改文章状态失败" });
    } else {
      res.send({
        code: 200,
        msg: "删除成功",
        data: result,
      });
    }
  });
});

// 恢复文章
// export const recoverPassageApi = (params) => request.post("/api/articlerecover", params);
router.post("/api/articlerecover", (req, res) => {
  const { id } = req.body;
  const sql = "UPDATE articles SET is_deleted = 0 WHERE id = ?";
  db.query(sql, [id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "修改文章状态失败" });
    } else {
      res.send({
        code: 200,
        msg: "恢复成功",
        data: result,
      });
    }
  });
});

// export const getCommentApi = (params) => request.post("/api/getComment", params);
// 根据文章id获取评论
router.post("/api/getComment", (req, res) => {
  const { article_id } = req.body;
  const sql = "SELECT * FROM comments WHERE article_id = ?";
  db.query(sql, [article_id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取评论信息失败" });
    } else {
      res.json(result);
    }
  });
});

// export const getAllCommentApi = () => request.post("/api/getAllComment");
// 获取全部评论
router.post("/api/getAllComment", (req, res) => {
  const sql = "SELECT * FROM comments";
  db.query(sql, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取评论信息失败" });
    } else {
      res.send({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// export const checkCommentApi = (params) => request.post("/api/checkComment", params);
// 审核评论
router.post("/api/checkComment", (req, res) => {
  const { id, approved } = req.body;
  console.log(req.body);
  const sql = "UPDATE comments SET approved = ? WHERE id = ?";
  db.query(sql, [approved, id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "审核评论失败" });
    } else {
      res.json({ message: "审核评论成功" });
    }
  });
});

// export const addCategoryApi = (params) => request.post("/api/category", params);
router.post("/api/category", (req, res) => {
  const { name } = req.body;
  console.log(req.body);
  const sql = "INSERT INTO categories (name) VALUES (?)";
  db.query(sql, [name], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "添加分类失败" });
    } else {
      res.send({
        code: 200,
        msg: "添加成功",
        data: result,
      });
    }
  });
});

// export const showCategoryApi = () => request.post("/api/categoryadd");
router.post("/api/categoryadd", (req, res) => {
  const sql = "SELECT * FROM categories";
  db.query(sql, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取分类信息失败" });
    } else {
      res.send({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// export const deleteCategoryApi = (params) => request.post("/api/categorydelete", params);
router.post("/api/categorydelete", (req, res) => {
  const { id } = req.body;
  console.log(req.body);
  const sql = "DELETE FROM categories WHERE id = ?";
  db.query(sql, [id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "删除分类失败" });
    } else {
      res.json({ message: "删除分类成功" });
    }
  });
});
 
// export const addLikeApi = (params) => request.post("/api/like", params);
router.post("/api/like", (req, res) => {
  const { article_id, likes_count } = req.body;
  console.log(req.body);
  const sql =
    "INSERT INTO articles_like (article_id, likes_count) VALUES (?, ?)";
  const values = [article_id, likes_count];
  db.query(sql, values, (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "保存点赞信息失败" });
    } else {
      // 获取点赞信息
      res.send({
        code: 200,
        msg: "保存成功",
        data: result,
      });
    }
  });
});

// export const getLikeApi = (params) => request.post("/api/getLike", params);
router.post("/api/getLike", (req, res) => {
  const { article_id } = req.body;
  const sql = "SELECT * FROM articles_like WHERE article_id = ?";
  db.query(sql, [article_id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "获取点赞信息失败" });
    } else {
      res.send({
        code: 200,
        msg: "获取成功",
        data: result,
      });
    }
  });
});

// export const addCountApi = (params) => request.post("/api/count", params);
router.post("/api/count", (req, res) => {
  // 根据articles表中的article_id字段，插入到views_count字段
  const { id, views_count } = req.body;
  console.log(req.body);
  const sql = "UPDATE articles SET views_count = ? WHERE id = ?";
  db.query(sql, [views_count, id], (err, result) => {
    if (err) {
      console.error(err);
      res.status(500).json({ message: "保存浏览量信息失败" });
    } else {
      res.json({ message: "保存浏览量信息成功" });
    }
  });
});

module.exports = router;
